package com.doyens.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doyens.reggie.domain.Category;
import com.doyens.reggie.domain.Setmeal;
import com.doyens.reggie.domain.SetmealDish;
import com.doyens.reggie.dto.SetmealDto;
import com.doyens.reggie.exception.CustomException;
import com.doyens.reggie.mapper.CategoryMapper;
import com.doyens.reggie.mapper.SetmealMapper;
import com.doyens.reggie.service.CategoryService;
import com.doyens.reggie.service.SetmealDishService;
import com.doyens.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public void saveSetmeal(SetmealDto setmealDto) {
        // 添加套餐表
        this.save(setmealDto);

        // 获取生成的id
        Long id = setmealDto.getId();

        // 添加关系表
        List<SetmealDish> dishList = setmealDto.getSetmealDishes();

        for (SetmealDish setmealDish : dishList) {
            setmealDish.setSetmealId(id);
        }

        // 批量添加  SetmealDish
        setmealDishService.saveBatch(dishList);
    }

    @Override
    public void delWithSetmealDish(List<Long> ids) { // 1 2 3
        // 先查询
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        wrapper.eq(Setmeal::getStatus,1);
        int count = this.count(wrapper);
        if(count > 0){
            throw  new CustomException("所选的套餐正在售卖，不能删除");
        }

        // 先删除关系表
        LambdaQueryWrapper<SetmealDish> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(wrapper2);

        // 再删除套餐表
        LambdaQueryWrapper<Setmeal> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.in(Setmeal::getId,ids);
        this.remove(wrapper3);
    }

    @Override
    public SetmealDto findById(long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        // 菜品集合没有值
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(setmealDishList);
        return setmealDto;
    }

    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
        // 1.根据套餐的id删除中间表的数据
        Long id = setmealDto.getId();
        setmealDishService.remove(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId,id));
        // 2.修改套餐表的数据
        this.updateById(setmealDto);

        // 3.添加中间表的数据
        List<SetmealDish> dishList = setmealDto.getSetmealDishes();

        for (SetmealDish setmealDish : dishList) {
            setmealDish.setSetmealId(id);
        }

        // 批量添加  SetmealDish
        setmealDishService.saveBatch(dishList);
    }
}
